# -*- coding: utf-8 -*-
# @时间：2023/11/7 16:00
# @作者：CJiaの可乐哥
# @文件名：role
# @软件：Pycharm2023
from typing import List

from ninja import Router, Schema, Field, ModelSchema, Query
from django.shortcuts import get_object_or_404
from system.models import Role

from utils.cj_curd import retrieve, delete, update, status

router = Router()


class Filters(Schema):
    id: str = Field(None, alias="id")
    name: str = Field(None, alias="name")
    status: bool = Field(None, alias="status")


class RoleSchemaIn(ModelSchema):
    class Config:
        model = Role
        model_exclude = ['id', 'create_datetime', 'update_datetime']


class RoleSchemaOut(ModelSchema):
    class Config:
        model = Role
        model_fields = "__all__"


@router.delete("/role/{role_id}")
def delete_menu(request, role_id: int):
    request.types = "sys:role:delete"
    # 调用删除方法
    result = delete(request, role_id, Role)
    # 返回结果
    return result


@router.put('/role/{role_id}', response=RoleSchemaOut)
def update_role(request, role_id: int, data: RoleSchemaIn):
    request.types = "sys:role:update"
    # 调用修改方法
    result = update(request, role_id, data, Role)
    # 返回结果
    return result


@router.put("/role/status/{role_id}")
def status_role(request, role_id: int):
    request.types = "sys:role:update"
    # 调用修改状态方法
    result = status(request, role_id, Role)
    # 返回结果
    return result


@router.get("/role/{role_id}", response=RoleSchemaOut)
def get_role(request, role_id: int):
    role = get_object_or_404(Role, id=role_id)
    return role


@router.get("/role", response=List[RoleSchemaOut])
def list_role(request, filters: Filters = Query(...)):
    qs = retrieve(request, Role, filters)
    return qs
